package com.ycsj.goldmedalnewconcept.master.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.PickerManager;
import com.ycsj.common.utils.DateUtils;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.util.LpUtils;
import com.ycsj.goldmedalnewconcept.master.adapter.StDataRvAdapter;
import com.ycsj.goldmedalnewconcept.master.bean.StDataInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 学生数据
 * Created by Administrator on 2019/4/15 0015.
 */

public class MasterStudentDataActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_edit)
    ImageView lvEdit;
    //新增总人数
    @BindView(R.id.tv_xzrs)
    TextView tvXzrs;
    //流失总人数
    @BindView(R.id.tv_lsrs)
    TextView tvLsrs;
    @BindView(R.id.ll_num_details)
    LinearLayout llNumDetails;
    @BindView(R.id.tv_start_time)
    TextView tvBeginTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;
    @BindView(R.id.ll_content)
    LinearLayout llContent;


    //空视图和头布局
    private View emptyView, headView;


    //列表适配器
    private StDataRvAdapter adapter;

    //数据源
    private List<StDataInfo> oldDatas;


    //开始时间和结束时间
    private String begindate, enddate;


    @Override
    protected int getContentLayout() {
        return R.layout.activity_master_student_data;
    }

    @Override
    protected void init() {
        initStateBar();

        //初始化标题
        initTitle();

        //初始化初次进入的时间
        initTime();

        //设置列表适配器
        initRv();
    }


    /**
     * 初始化标题
     */
    private void initTitle() {
        tvTitle.setText("学生数据");
        RelativeLayout.LayoutParams lp = LpUtils.getRelatLp(UiUtils.dp2px(36), UiUtils.dp2px(36));
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        lvEdit.setLayoutParams(lp);
        lvEdit.setImageResource(R.drawable.data_analyze);
        lvEdit.setVisibility(View.VISIBLE);
    }


    /**
     * 初始化开始时间和结束时间.默认开始时间为当前月份的前一个
     */
    private void initTime() {
        enddate = DateUtils.getTime(new Date(), "yyyy-MM-dd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        begindate = format.format(m);

        //设置初始时间
        tvBeginTime.setText(DateUtils.getFmTime(begindate));
        tvEndTime.setText(DateUtils.getFmTime(enddate));
    }


    /**
     * 设置列表适配器
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            StDataInfo info = new StDataInfo();
            oldDatas.add(info);
        }
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        headView = View.inflate(this, R.layout.headview_rv_st_data, null);


        adapter = new StDataRvAdapter(this, R.layout.item_rv_st_data, oldDatas);
        adapter.addHeaderView(headView);

        rlLoading.setVisibility(View.GONE);
        BqaManager.setRv(emptyView, this, adapter, rv);
    }


    /**
     * 请求服务器获取数据
     *
     * @param method
     */
//    private void http(int method) {
//        Map<String, String> map = new HashMap<>();
//        map.put("type", "7");
//        map.put("acc", acc);
//        map.put("role", role);
//        map.put("classnum", classNum);
//        map.put("begindate", begindate);
//        map.put("enddate", enddate);
//        map.put("apptoken", apptoken);
//        map.put("platform", "8");
//
//        HttpUtils.post(HttpAddress.STUDENTANALY, map, this, new HttpUtils.CallBack() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                BqaManager.setErrorNet(rlLoading, null, method, adapter);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
//                    rlLoading.setVisibility(View.GONE);
//                }
//                if (llContent != null && llContent.getVisibility() != View.VISIBLE) {
//                    llContent.setVisibility(View.VISIBLE);
//                }
//
//                if (!TextUtils.isEmpty(response)) {
//                    StDataInfo info = null;
//                    try {
//                        info = GsonUtil.jsonToBean(response, StDataInfo.class);
//                    } catch (Exception e) {
//                        Log.i("test", Constants.PARSEDATAERROR);
//                    }
//
//                    if (null != info) {
//                        if ("200".equals(info.getState())) {
//                            List<StDataInfo> newDatas = info.getData();
//                            if (newDatas != null) {
//                                if (method == BqaManager.NORML) {
//                                    oldDatas.clear();
//                                    oldDatas.addAll(newDatas);
//                                    BqaManager.setRv(emptyView, MasterStudentDataActivity.this, adapter, rv);
//                                } else {
//                                    //更新数据
//                                    BqaManager.updateRvBySmf(method, null, rv, oldDatas, newDatas, adapter);
//                                    adapter.setEnableLoadMore(false);
//                                }
//                            }
//
//                        } else {
//                           ToastUtils.showShort(Constants.ERRORSTATE + info.getState());
//                            Log.i("test", Constants.ERRORSTATE + info.getState());
//                        }
//                    } else {
//                        Log.i("test", Constants.EMPTY);
//                    }
//                } else {
//                    Log.i("test", Constants.ERROR);
//                }
//            }
//        });
//    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_edit, R.id.tv_start_time, R.id.tv_end_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.lv_edit:
                //数据示意图
                UiUtils.startActivity(this, MasterShowStDataActivity.class);
                break;
            case R.id.tv_start_time:
                //开始时间
                showTimePicker(0);
                break;
            case R.id.tv_end_time:
                //结束时间
                showTimePicker(1);
                break;
        }
    }

    /**
     * 弹出选择时间的日期选择期
     *
     * @param pos
     */
    private void showTimePicker(final int pos) {
        PickerManager.showPickerByYeaerAndMonth(this, new PickerManager.onSelectedListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (pos == 0) {

                    String chooseBeginDate = DateUtils.getTime(date, "yyyy-MM-dd");

                    //比较时间差,然后刷新页面数据
                    long end = DateUtils.getTime(enddate, "yyyy-MM-dd");
                    long start = DateUtils.getTime(chooseBeginDate, "yyyy-MM-dd");

                    //计算选择的开始和结束时间,转化为date格式
                    Date startDate = null;
                    Date endDate = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    try {
                        startDate = sdf.parse(chooseBeginDate);
                        endDate = sdf.parse(enddate);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    if (end <= start) {
                        ToastUtils.showShort("选择的开始时间不能大于结束时间");
                        return;
                    } else if (DateUtils.getDiffMonth(startDate, endDate) < 1) {
                        //小于1个月
                        ToastUtils.showShort("选择的时间间隔不能小于1个月");
                        return;
                    } else if (DateUtils.getDiffMonth(startDate, endDate) > 6) {
                        //大于6个月
                        ToastUtils.showShort("选择的时间间隔不能超过6个月");
                        return;
                    } else {

                        begindate = DateUtils.getTime(date, "yyyy-MM-dd");

                        tvBeginTime.setText(DateUtils.getFmTime(begindate));

                        //刷新数据
//                        http(BqaManager.PULLREFRESH);
                    }
                } else if (pos == 1) {

                    String chooseEndDate = DateUtils.getTime(date, "yyyy-MM-dd");

                    //比较时间差,然后刷新页面数据
                    long end = DateUtils.getTime(chooseEndDate, "yyyy-MM-dd");
                    long start = DateUtils.getTime(begindate, "yyyy-MM-dd");

                    //计算选择的开始和结束时间,转化为date格式
                    Date startDate = null;
                    Date endDate = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    try {
                        startDate = sdf.parse(begindate);
                        endDate = sdf.parse(chooseEndDate);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    if (end <= start) {
                        ToastUtils.showShort("选择的结束时间不能小于开始时间");
                    } else if (DateUtils.getDiffMonth(startDate, endDate) < 1) {
                        //小于1个月
                        ToastUtils.showShort("选择的时间间隔不能小于1个月");
                        return;
                    } else if (DateUtils.getDiffMonth(startDate, endDate) > 6) {
                        //大于6个月
                        ToastUtils.showShort("选择的时间间隔不能超过6个月");
                        return;
                    } else {
                        enddate = DateUtils.getTime(date, "yyyy-MM-dd");
                        tvEndTime.setText(DateUtils.getFmTime(enddate));
                        //刷新数据
//                        http(BqaManager.PULLREFRESH);
                    }
                }

            }
        });
    }
}
